package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.DestinationSearchQueryObject;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelsQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    //相对应的Id去查出来top3的标签
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    //游记
    @Reference
    private ITravlesService travlesService;

    //查时间
    @Reference
    private IDestinationMouthTimeService destinationMouthTimeService;
    //节假日
    @Reference
    private IDestinationTimeVacationService timeVacationService;
    //主题
    @Reference
    private IDestinationThemeDetailService themeDetailService;
    //查主题
    @Reference
    private IDestinationThemeService themeService;
    //查出广州下面前三的景点
    @Reference
    private IScenicSpotService scenicSpotService;
    //查标签
    @Reference
    private IDestinationThemeDetailService destinationThemeDetailService;
    @RequestMapping("")
    public String index(Model model) {
        List <Region> regions = regionService.queryHotRegions();
        model.addAttribute( "hotRegions", regions );

        //显示月份的
        List<DestinationMouthTime> dmts = destinationMouthTimeService.selectAll();
        List<DestinationMouthTime> mous = dmts.subList(1, 12);
        model.addAttribute("mou",mous);
        //显示主题推荐的   显示所有
        List<DestinationTheme> themes = themeService.selectAll();
        List<DestinationTheme> themeList = themes.subList(1, 3);
        model.addAttribute("themes",themeList);

        return "destination/index";

    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId) {
     List<Destination> destinations=   destinationService.queryHotDestByRegionId( regionId );
        List <Destination> reft = destinations.subList( 0, destinations.size() / 2 + 1 );
        List <Destination> right = destinations.subList( destinations.size() / 2 + 1, destinations.size() );
        model.addAttribute( "leftDests",reft );
        model.addAttribute( "rightDests",right );
        model.addAttribute( "regionId",regionId );
        return "destination/hotdestTpl";
    }
    //查询目的地下的所有攻略和游记
    @RequestMapping("/guide")
    public String guide(Model model,Long id) {
        List <Destination> toasts = destinationService.getToasts( id );
        Destination destination = toasts.remove( toasts.size() - 1 );
        model.addAttribute( "toasts" ,toasts);
        model.addAttribute( "dest" ,destination);
        List <StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId( id );
        model.addAttribute( "catalogs" ,catalogs);
        List<StrategyDetail> strategyDetails=  strategyDetailService.selectViewNumTop3(id);
        model.addAttribute( "strategyDetails" ,strategyDetails);

        return "destination/guide";
    }
    @RequestMapping("/surveyPage")
    public String surveyPage(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo) {
        List <Destination> toasts = destinationService.getToasts( qo.getDestId() );
        Destination destination = toasts.remove( toasts.size() - 1 );
        model.addAttribute( "toasts" ,toasts);
        model.addAttribute( "dest" ,destination);

        //查询当前目的地下的所有攻略
        List <StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId( qo.getDestId() );


        return "destination/survey";
    }
    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo) {
        List <StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId( qo.getDestId() );
        model.addAttribute( "catalogs" ,catalogs);
        StrategyCatalog   catalog = strategyCatalogService.queryCatalogByCatalogId(qo.getCatalogId()  );
        model.addAttribute( "catalog" ,catalog);
        StrategyDetail detail = catalog.getDetails().get( 0 );
        StrategyContent strategyContent = strategyDetailService.queryContent( detail.getId() );
        detail.setStrategyContent( strategyContent );
        model.addAttribute( "detail" ,detail);
        return "destination/surveyTpl";
    }
    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo")TravelsQuery qo) {
        PageInfo pageInfo=travlesService.query(qo  );
        model.addAttribute( "pageInfo" ,pageInfo);
        return "destination/travelTpl";
    }

    /**
     * 高级地点查询
     * @param model 视图解析器
     * @param qo 查询条件
     * @return
     */
    @RequestMapping("/destseach")
    public String destseach(Model model,@ModelAttribute("qo") DestinationSearchQueryObject qo,Integer dtd1Id){
        //月份  前面12个
        List<DestinationMouthTime> dmts = destinationMouthTimeService.selectAll();
        List<DestinationMouthTime> mouthTimes = dmts.subList(0, 12);
        model.addAttribute("dmts",mouthTimes);
        //节假日   13到最后面的
        List<DestinationMouthTime> vas = dmts.subList(13, dmts.size());
        model.addAttribute("vas",vas);
        //主题
        //1 全年适宜dest_theme_id=1
        List<DestinationThemeDetail> dtd1 = themeDetailService.selectByDestThemeId(1L);
        model.addAttribute("dtd1",dtd1);

        //2 季节dest_theme_id=2
        List<DestinationThemeDetail> dtd2 = themeDetailService.selectByDestThemeId(2L);
        model.addAttribute("dtd2",dtd2);

        //3 出行方式适宜dest_theme_id=3
        List<DestinationThemeDetail> dtd3 = themeDetailService.selectByDestThemeId(3L);
        model.addAttribute("dtd3",dtd3);
        /**
         * 月份
         * 节假日
         * 各种方式
         * 出行天数
         */
        System.err.println("月份"+qo.getMouthid());
        System.err.println("节假日"+qo.getVacationid());

        System.err.println("各种方式"+qo.getDtd1Id());
        System.err.println("出行天数"+qo.getVacatinonDays());
        //根据qo来查询出我需要的结果

        if (dtd1Id!=null){
            qo.setDtd1Id(dtd1Id);
        }


        PageInfo pageInfo = destinationService.queryForListAccordingCondition(qo);
        //得到前5个的Destid  然后去查出相对应的前三个攻略id

        //广州游记的Top3
        List<ScenicSpot> spotList=scenicSpotService.queryForTop3BySpotcomment();

        //查询这个id下面的游记 然后把标题显示出来
        Travel  travel = travlesService.queryforTravelByDestId(3261L);


        model.addAttribute("travel",travel); //旅游地点
        model.addAttribute("spotList",spotList);//旅游目的地前三的收藏景点
        model.addAttribute("pageInfo",pageInfo);
        return "destination/destFilter";
    }
    //当季推荐

    /**
     * 当即推选
     * @param regionId 月份的Id
     * @param model 视图
     * @return
     */
    @RequestMapping("getDestByMouth")
    public String getDestByMouth(Long regionId,Model model){
        List<Destination> destinations=destinationService.selectDestByMouthId(regionId);
        model.addAttribute("destinations",destinations);


        model.addAttribute("regionId",regionId);
        return "destination/mouthdestTpl";
    }

    /**
     * 用来查主题推选的 就是
     * @param regionId 地点的ID
     * @param model 视图解析器
     * @return
     */
    @RequestMapping("getDestByThemeId")
    public String getDestByTheme(Long regionId,Model model){
        List<DestinationThemeDetail> themeDetails=null;
        themeDetails = destinationThemeDetailService.selectByDestThemeId(regionId);

        if (themeDetails.size()>8){
           themeDetails = themeDetails.subList(1, 8);
        }

        model.addAttribute("themeDetails",themeDetails);
        model.addAttribute("regionId",regionId);
        return "destination/themedestTpl";
    }
}
